<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">角色英文名</label>
        <div class="layui-input-inline">
            <input type="text" name="name" lay-verify="name" lay-reqtext="角色名是必填项" placeholder="角色名必须以ROLE_开头"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">角色中文名</label>
        <div class="layui-input-inline">
            <input type="text" name="nameZh" lay-verify="required" placeholder="请输入角色中文名" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1" id="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script src="/static/layuiadmin/layui/layui.js"></script>

<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , $ = layui.$;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            name: function (value) {
                var reg=/^ROLE_\w+$/;
                if (!reg.test(value)) {
                    return '角色名必须以ROLE_开头，比如ROLE_admin';
                }
            }
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        form.on('submit(demo1)', function (data) {
            $.ajax({
                url: '/addRole'
                , method: 'post'
                , data: data.field
                , success: function (res) {
                    layer.msg("添加成功");
                    setTimeout(function () {
                        window.parent.location.reload();//修改成功后刷新父界面
                    }, 1000);
                }
            });
            return false;
        });
    })
    ;
</script>
</html>